<!DOCTYPE html>
<html>


<!-- Mirrored from www.gzsxt.cn/theme/hplus/table_basic.html by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 20 Jan 2016 14:20:01 GMT -->
<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>办公系统 - 基础表格</title>
    <meta name="keywords" content="办公系统">
    <meta name="description" content="办公系统">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet">
    <link href="css/plugins/select/bootstrap-select.min.css"
          rel="stylesheet">
    <link rel="stylesheet" href="css/plugins/zTreeStyle/zTreeStyle.css"/>

</head>

<body class="gray-bg">
<div class="wrapper2 wrapper-content2 animated fadeInRight">
    <div class="row">
        <div class="col-sm-6">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>资源管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="zTreeDemoBackground left" style="font-size: 16px">
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>资源添加</h5>
                </div>
                <div class="ibox-content">
                    <form id="dataForm" class="form-horizontal">
						<input type="hidden" name="id"/>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">菜单资源名称：</label>

                            <div class="col-sm-7">
                                <input type="text" name="name" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">父菜单：</label>

                            <div class="col-sm-7">
                                <select id="pmenu" name="pid" class="selectpicker form-control">
									<option value="0">---请选择父菜单---</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">菜单资源路径：</label>

                            <div class="col-sm-7">
                                <input name="url" type="text" class="form-control">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-4 control-label">备注：</label>
                            <div class="col-sm-7">
                                <textarea name="remark" class="form-control"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-8">
                                <button class="btn btn-sm btn-white btnSave">
                                    <i class="fa fa-save"></i> 保存
                                </button>
                                <button class="btn btn-sm btn-white btnReset">
                                    <i class="fa fa-undo"></i> 重置
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>

</div>
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>
<script src="js/plugins/select/bootstrap-select.min.js"></script>
<script src="js/plugins/sweetalert/sweetalert.min.js"></script>
<script src="js/plugins/ztree/jquery.ztree.core.min.js"></script>
<script src="js/plugins/ztree/jquery.ztree.exedit.js"></script>
<script>
    $(document)
        .ready(
            function () {
                // 设置按钮的样式
                $('.selectpicker').selectpicker('setStyle',
                    'btn-white').selectpicker('setStyle',
                    'btn-sm');

            });
</script>
<SCRIPT type="text/javascript">
    var setting = {
        async: {
            enable: true,
            url: "/sources/standerdata",
            autoParam: ["id"]
        },
        view: {
            addHoverDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + "_a");
                aObj.attr("href", "javascript:void(0);").removeAttr("target");
                if (treeNode.editNameFlag
                    || $("#btnGroup" + treeNode.tId).length > 0)
                    return;
                var s = '<span id="btnGroup' + treeNode.tId + '">';
                if (treeNode.level == 1) {
                    if (treeNode.children.length == 0) {
                        s += '<span class="button remove" onclick="deleteNode('
                            + treeNode.id + ')"></span>';
                    }
                } else if (treeNode.level == 2) {
                    s += '<span class="button edit" onclick="editNode('
                        + treeNode.id + ')" ></span>';
                    s += '<span class="button remove" onclick="deleteNode('
                        + treeNode.id + ')" ></span>';
                }
                s += '</span>';
                aObj.append(s);
            },
            removeHoverDom: function (treeId, treeNode) {
                $("#btnGroup" + treeNode.tId).remove();
            }
        }

    };
    let methodName = "save";
	loadParents();
	//加载父菜单
	function loadParents() {
		$.getJSON("/sources/getByPid",{"pid":"1"},function (response) {
			$(response).each(function () {

				let $option = `<option value="`+this.id+`">`+this.name+`</option>`;
				$("#pmenu").append($option).selectpicker('refresh');;
			});
		});
	}
	$(".btnReset").click(function () {
		reset();
	});
	$(".btnSave").click(function () {
		let data = $("#dataForm").serialize();
		$.post("/sources/"+methodName,data,function (response) {
			let message = "添加";
			if (methodName=="update"){
				message = "修改";
			}
			if (response == "success") {
				swal(message+"成功", "为您刷新页面...", "success");
				reset();
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				treeObj.reAsyncChildNodes(null, "refresh");
			} else {
				swal(message+"失败", "请检查数据有效性...", "error");
			}
		},"text")
	});
	//重置
	function reset() {
		$("#dataForm")[0].reset();
		methodName="save";
		$("input[name=id]").val("0");
	}
	
    //编辑
    function editNode(id) {
    	$.getJSON("/sources/get",{"id":id},function (response) {
			$("input[name=name]").val(response.name);
			$("input[name=id]").val(response.id);
			$("input[name=url]").val(response.url);
			$("textarea[name=remark]").val(response.remark);
			$("select[name=pid]").selectpicker("val",response.pid);
		})
		methodName="update";
    }

    //删除
    function deleteNode(id) {
        swal({
            title: "您确定要改修该角色状态吗？",
            text: "改修后将将，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false,
            cancelButtonText: "取消"
        }, function () {//此函数是点击删除执行的函数
            //这里写ajax代码
            // 以下是成功的提示框，请在ajax回调函数中执行
            $.post("/sources/remove",{"id":id},function (response) {
                if (response=="success"){
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
                    var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                    treeObj.reAsyncChildNodes(null, "refresh");
                }else {
                    swal("删除失败！", "请稍后重试...", "success");
                }
            });

        });
        //ajax请求台
        /* $.post("/crmpro/sources/remove", "id=" + id, function(data) {
            // 刷新数据
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            treeObj.reAsyncChildNodes(null, "refresh");
        }, "text"); */
    }

    $(document).ready(function () {
        $.fn.zTree.init($("#treeDemo"), setting);
    });
</SCRIPT>
</body>


</html>
